using System;
using System.Collections.Generic;
using System.Text;
using TTDotNet.Util;

namespace TTDotNet.Engine.Terrain
{
    public class SectorStrip
    {
        List<Sector> sectorList;
        bool valid;

        public SectorStrip(Sector start, Sector finish)
        {
            sectorList = new List<Sector>();
            valid = false;
            PopulateList(start, finish);
        }

        public bool IsValid()
        {
            return valid;   
        }

        public void PopulateList(Sector start, Sector finish)
        {
            RotateType direction = RotateType.Zero;

            if ((start.coord.X != finish.coord.X) && (start.coord.Z != finish.coord.Z))
            {
                return;
            }

            RotateType oppToStart = (RotateType)(((int)start.rotType + 2) % 4);

            if((start.rotType != finish.rotType) && (finish.rotType != oppToStart)) {
                return;
            }


            valid = true;

            Axis moved = Axis.X;
            int adder = 0;

            if (start.coord.Get(Axis.X) < finish.coord.Get(Axis.X)) { direction = RotateType.Ninety; adder = 1; }
            else if (start.coord.Get(Axis.X) > finish.coord.Get(Axis.X)) { direction = RotateType.TwoSeventy; adder = -1; }
            else if (start.coord.Get(Axis.Z) < finish.coord.Get(Axis.Z)) { direction = RotateType.Zero; moved = Axis.Z; adder = 1; }
            else if (start.coord.Get(Axis.Z) > finish.coord.Get(Axis.Z)) { direction = RotateType.OneEighty; moved = Axis.Z; adder = -1; }

            //Check triangles clicked in correspond to the direction being dragged.
            if (!(start.Equals(finish)))
            {
                if (((int)direction + (int)start.rotType) % 2 == 1 || ((int)direction + (int)finish.rotType) % 2 == 1)
                {
                    return;
                }
            }

            Sector current = new Sector(new Coordinate(-1, -1), RotateType.Zero);

            Coordinate currCoord = new Coordinate(start.coord.X, start.coord.Z);
            RotateType currRot = start.rotType;

            

            bool firstHalf = true;

            if (start.rotType == direction) { firstHalf = false; }

            while (!(current.Equals(finish)))
            {
                current = new Sector(currCoord, currRot);
                sectorList.Add(current);
                
                currRot = (RotateType)(((int)currRot + 2) % 4);
                if (!firstHalf)
                {
                    if (moved   == Axis.X) { currCoord = new Coordinate(currCoord.X + adder, currCoord.Z); }
                    else { currCoord = new Coordinate(currCoord.X, currCoord.Z + adder); }
                }
                firstHalf = !firstHalf;
                
            }
        }

        public IEnumerator<Sector> GetEnumerator()
        {
            foreach (Sector s in sectorList)
            {
                yield return s;
            }
        }

    }
}
